package com.ycy.sort;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Author: ycy
 * @CreateDate: 2020/9/10 12:05
 */
public class MergeSort1 {

  @Test
  public void test1() {
    int[] a = {2};
    int[] b = {3};
    int[] x = mergeSort(a, b);
    System.out.println(Arrays.toString(x));

    System.out.println(check(x));
  }

  public boolean check(int[] a) {
    int pre = a[0];
    for (int i = 1; i < a.length; i++) {
      if (pre > a[i]) return false;
      pre = a[i];
    }
    return true;
  }

  public int[] mergeSort(int[] a, int[] b) {

    int[] newArr = new int[a.length + b.length];

    int ai = 0;
    int bi = 0;
    int cur = 0;

    while (ai < a.length || bi < b.length) {
      //取a数组
      if (bi >= b.length || (ai < a.length && a[ai] < b[bi])) {
        newArr[cur++] = a[ai++];
      } else {
        newArr[cur++] = b[bi++];
      }


    }

    return newArr;
  }

}
